package socket2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

/**
 * @classname:WriteOrReadMsg
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/21 19:35
 * @version: 0.1
 * @since:1.8
 */
public class WriteOrReadMsg {
    private WriteOrReadMsg(){}
    private static boolean flag = true;
    private static final Scanner input ;
    static {
        input = new Scanner(System.in);
    }
    public static void readMsg(Socket socket){

        try {
            while (true) {
                DataInputStream inputStream = new DataInputStream(socket.getInputStream());
                System.out.println(inputStream.readUTF());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static  void writeMsg(Socket socket){
        try {
            while (flag) {
                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                String name = Thread.currentThread().getName();
                String str = input.nextLine();
                if(str.equals("seeyou")){
                    flag =false;
                    break;
                }
                outputStream.writeUTF(name+"-------------------\n"+str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
